package com.suneast.suneaststore.modules.goodsdetail.subfragment;

import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.suneast.suneaststore.R;
import com.suneast.suneaststore.modules.goodsdetail.GoodsHandler;
import com.suneast.suneaststore.modules.goodsdetail.adapter.SpecificationAdapter;
import com.suneast.suneaststore.bean.goodsdetail.SpecificationBean;
import com.suneast.suneaststore.modules.goodsdetail.event.BottomPageChangeEvent;
import com.suneast.suneaststore.modules.goodsdetail.event.SubCateLoaded;
import com.suneast.suneaststore.ui.fragments.BaseFragment;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

import butterknife.BindView;

public class GoodsSpecsListFragment extends BaseFragment {

    @Override
    public @LayoutRes
    int setLayout() {
        return R.layout.fragment_goods_specification;
    }


    ArrayList<SpecificationBean> dataList;

    @BindView(R.id.rv_specification)
    RecyclerView specificationRv;

    SpecificationAdapter adapter;

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View rootView) {
        EventBus.getDefault().register(this);
        dataList = new ArrayList<>();
        specificationRv.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter = new SpecificationAdapter(R.layout.item_goods_specification);
        specificationRv.setAdapter(adapter);
        Bundle bundle = getArguments();
        if (bundle != null) {
            int id = bundle.getInt("id");
            GoodsHandler.requestGoodsParamet(id);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onPageChange(BottomPageChangeEvent event) {
        if (event.getIndex() == 1) {
            if (event.getResponse() != null) {
                JSONObject rootJObj = JSONObject.parseObject(event.getResponse());
                if (rootJObj.getIntValue("code") == 1) {
                    JSONArray jArray = rootJObj.getJSONArray("data");
                    if (jArray != null && jArray.size() > 0) {
                        for (int i = 0, length = jArray.size(); i < length; i++) {
                            JSONObject jObj = jArray.getJSONObject(i);
                            dataList.add(new SpecificationBean(jObj.getString("label"), jObj.getString("labelname")));
                        }
                        adapter.addData(dataList);
                        EventBus.getDefault().post(new SubCateLoaded());
                    }
                }
            }
        }
    }

    @Override
    public void onDestroyView() {
        EventBus.getDefault().unregister(this);
        super.onDestroyView();
    }
}
